<?php
namespace logic\admin\shop;
use library\common\collections\ArrayUtils;
use model\ShopGoodsVariant;

/**
 * User: lsx
 * Date: 2017/6/2
 * Time: 10:28
 * Progress:0%
 */
class LShopGoodsVariant
{
    private $ShopGoodsVariant;
    public function __construct()
    {
        $this->ShopGoodsVariant=new ShopGoodsVariant();
    }

    public function logic_index_data(){
        $data=$this->ShopGoodsVariant->where(['flag_delete'=>0])->select();
        //$list_tree = $this->tree_asc($data);

        return $data;
    }

    public function index_data($param)
    {
        $where = array();
        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['name'] = array('like', "%{$keywords}%"); //查询条件
        }

        $where['flag_delete'] = 0;

        $page_size = $param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['ordinal'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['id'] = 'desc'; //默认排序
        }
        if (!empty($param["goods_id"])){
            $where['goods_id'] = $param["goods_id"];
        }
        //获取排序字段------------------------------------------------------------结束

        $total = $this->ShopGoodsVariant->where(['flag_delete' => 0])->count();
        $rows = $this->ShopGoodsVariant
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();
        return array(
            'total' => $total,
            'rows' => $rows
        );
    }

    public function logic_insert($param){
        $res=$this->ShopGoodsVariant->insert($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>"添加成功"
            );
        }else{
            return array(
                'success'=>false,
                'message'=>"添加失败"
            );
        }
    }

    public function logic_edit($id){
        $item=$this->ShopGoodsVariant->where(['id'=>$id])->find();
        $item=$item->getData();
        return $item;
    }

    public function logic_update($param){
        $res=$this->ShopGoodsVariant->update($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>"修改成功"
            );
        }else{
            return array(
                'success'=>false,
                'message'=>"修改失败"
            );
        }
    }

    public function logic_delete($id){
        $res=$this->ShopGoodsVariant->where(['id'=>$id])->setField(["flag_delete"=>1]);
        if($res){
            return array(
                'success'=>true,
                'message'=>"删除成功"
            );
        }else{
            return array(
                'success'=>false,
                'message'=>"删除失败"
            );
        }
    }

//    public function logic_cate_treeview_data(){
//        $where['flag_use'] = 1;
//        $where['flag_delete'] = 0;
//        $result = $this->ShopGoodsVariant->where($where)->field("id,parent_id,name as text")->order('sort asc')->select();
//        $list = json_decode(json_encode($result),true);
//        return ArrayUtils::arrayToTree($list,'id','parent_id','nodes','0')['nodes'];
//    }

}